package com.knoya.chejian.pda.module.ruku

import android.content.Context
import android.util.Log
import com.knoya.chejian.pda.contacts.Contacts
import com.knoya.chejian.pda.minterface.OnUserDataInterface
import com.knoya.chejian.pda.util.HttpUtil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.xutils.http.RequestParams
import org.xutils.x

class RukuM {

    fun getdata(context : Context, code : String, pcode : String, onuserdatainterface : OnUserDataInterface){
        var stat = "category"
        when(code){
            "按订单" -> {
                stat = "order"
            }
            "按柜号" -> {
                stat = "cabinet"
            }
            "按包号" ->{
                stat = "package"
            }
        }
        val params = RequestParams(Contacts.RUKU_XIAZAI)
        HttpUtil.getparams(context, params)
        HttpUtil.addtoken(params)
        params.addBodyParameter("bath", stat.trim(), Contacts.BOUNDARY)
        params.addBodyParameter("pcode", pcode.trim(), Contacts.BOUNDARY)
        params.addHeader("ACCEPT", "application/json")
        x.http().post<String>(params, HttpUtil.Call(context,onuserdatainterface))
    }

    fun ruku(context : Context, warehouse : String, position : String,mlist: List<RukuPcodeBean>, onuserdatainterface : OnUserDataInterface){
        val params = RequestParams(Contacts.RUKU)
        HttpUtil.getparams(context, params)
        HttpUtil.addtoken(params)

        var jo = JSONObject()
        var js= JSONArray()
        try {

            for (i in mlist.indices){
                var jo = JSONObject()
                jo.put("pcode",mlist[i].pcode)
                jo.put("warehouse",warehouse)
                jo.put("position",position)
                js.put(jo)
            }
            jo.put("package",js.toString())
        }catch ( e : JSONException){
            e.printStackTrace()
        }
//        params.addBodyParameter("package",js.toString(), Contacts.BOUNDARY)
        params.bodyContent = jo.toString()
        Log.d("TAAAG",jo.toString())
        params.addHeader("Content-Type", "application/json")

//        params.addBodyParameter("warehouse", warehouse.trim(), Contacts.BOUNDARY)
//        params.addBodyParameter("position",position.trim(),Contacts.BOUNDARY)
        for (i in mlist.indices){
            params.addBodyParameter("pcodes[$i]",mlist[i].pcode.trim(),Contacts.BOUNDARY)
        }
        params.addHeader("ACCEPT", "application/json")
        x.http().post<String>(params, HttpUtil.Call(context,onuserdatainterface))
    }
}